package com.crrc.dataparse.templatepara.datatrans.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

public class TyStreams {
    private String source = "";
    private StringBuilder goal = new StringBuilder();
    private boolean execute = true;
    private int falseLayer = 0;
    private Map<String, String> temporary = new HashMap();
    private int offset = 0;

    public TyStreams() {
    }

    public TyStreams setSource(String value) {
        if (!this.execute) {
            return this;
        } else {
            this.source = value;
            return this;
        }
    }

    public TyStreams setOffset(int num) {
        if (!this.execute) {
            return this;
        } else {
            this.offset = num;
            return this;
        }
    }

    public TyStreams addOffset(int num) {
        if (!this.execute) {
            return this;
        } else {
            this.offset += num;
            return this;
        }
    }

    public TyStreams add(String value) {
        if (!this.execute) {
            return this;
        } else {
            this.goal.append(value);
            return this;
        }
    }

    public TyStreams deleteLastAndAdd(String value) {
        if (!this.execute) {
            return this;
        } else {
            this.goal.deleteCharAt(this.goal.length() - 1);
            this.goal.append(value);
            return this;
        }
    }

    public TyStreams methodAdd(String name, String value, int num) {
        if (!this.execute) {
            return this;
        } else {
            this.goal.append("\"").append(name).append("\":").append(value);
            this.offset = num;
            return this;
        }
    }

    public TyStreams methodAdd(String name, String value) {
        if (!this.execute) {
            return this;
        } else {
            this.goal.append("\"").append(name).append("\":").append(value);
            return this;
        }
    }

    public TyStreams ifLogic(int ifBegin, int ifNum, int equal) {
        if (!this.execute) {
            ++this.falseLayer;
        } else if (equal != Integer.parseInt(this.source.substring(ifBegin, ifBegin + ifNum), 2)) {
            this.execute = false;
        }

        return this;
    }

    public TyStreams ifLogic(int ifBegin, int ifNum, String condition, int num) {
        if (!this.execute) {
            ++this.falseLayer;
        } else if (">".equals(condition)) {
            if (num < Integer.parseInt(this.source.substring(ifBegin, ifBegin + ifNum), 2)) {
                this.execute = false;
            }
        } else if ("<".equals(condition) && num > Integer.parseInt(this.source.substring(ifBegin, ifBegin + ifNum), 2)) {
            this.execute = false;
        }

        return this;
    }

    public TyStreams ifLogic(String name, int equal) {
        if (!this.execute) {
            ++this.falseLayer;
        } else if (this.temporary.get(name) == null) {
            this.execute = false;
        } else if (equal != Integer.parseInt((String)this.temporary.get(name))) {
            this.execute = false;
        }

        return this;
    }

    public TyStreams ifLogic(String name, String condition, int num) {
        if (!this.execute) {
            ++this.falseLayer;
        } else if (this.temporary.get(name) == null) {
            this.execute = false;
        } else if ("<".equals(condition)) {
            if (num < Integer.parseInt((String)this.temporary.get(name))) {
                this.execute = false;
            }
        } else if (">".equals(condition) && num > Integer.parseInt((String)this.temporary.get(name))) {
            this.execute = false;
        }

        return this;
    }

    public TyStreams end() {
        if (this.falseLayer == 0) {
            this.execute = true;
        } else {
            --this.falseLayer;
        }

        return this;
    }

    public TyStreams methodOut(String name, int begin, int num, String trans) {
        if (!this.execute) {
            return this;
        } else {
            String data = String.valueOf(this.source.substring(begin, begin + num));
            this.offset = begin + num;
            String[] conmands = trans.split("&");
            String result = this.command(conmands, data);
            this.goal.append("\"").append(name).append("\":").append("\"").append(result).append("\",");
            this.temporary.put(name, result);
            return this;
        }
    }

    public TyStreams methodOut(String name, int num, String trans) {
        if (!this.execute) {
            return this;
        } else {
            String data = String.valueOf(this.source.substring(this.offset, this.offset + num));
            this.offset += num;
            String[] conmands = trans.split("&");
            String result = this.command(conmands, data);
            this.goal.append("\"").append(name).append("\":").append("\"").append(result).append("\",");
            this.temporary.put(name, result);
            return this;
        }
    }

    public TyStreams methodOut(String name, int num, String trans, int afterEmptyNum) {
        if (!this.execute) {
            return this;
        } else {
            String data = String.valueOf(this.source.substring(this.offset, this.offset + num));
            this.offset += num + afterEmptyNum;
            String[] conmands = trans.split("&");
            String result = this.command(conmands, data);
            this.goal.append("\"").append(name).append("\":").append("\"").append(result).append("\",");
            this.temporary.put(name, result);
            return this;
        }
    }

    public TyStreams methodOut(int beforeEmptyNum, String name, int num, String trans) {
        if (!this.execute) {
            return this;
        } else {
            String data = String.valueOf(this.source.substring(beforeEmptyNum + this.offset, beforeEmptyNum + this.offset + num));
            this.offset += num + beforeEmptyNum;
            String[] conmands = trans.split("&");
            String result = this.command(conmands, data);
            this.goal.append("\"").append(name).append("\":").append("\"").append(result).append("\",");
            this.temporary.put(name, result);
            return this;
        }
    }

    public String out() {
        return this.goal.toString();
    }

    public String outWithJson() {
        this.goal.insert(0, "{");
        String result = this.goal.toString();
        if (result.endsWith(",")) {
            this.goal.deleteCharAt(this.goal.length() - 1);
        }

        this.goal.append("}");
        return this.goal.toString();
    }

    public int outOffset() {
        return this.offset;
    }

    private String command(String[] conmands, String value) {
        String result = value;
        String number = "";
        String[] var5 = conmands;
        int var6 = conmands.length;

        for(int var7 = 0; var7 < var6; ++var7) {
            String conmand = var5[var7];
            if (conmand.contains(">>")) {
                number = conmand.substring(2);
                conmand = ">>";
            } else {
                if (conmand.contains("-_byte")) {
                    continue;
                }

                if (conmand.contains("-_")) {
                    number = conmand.substring(2);
                    conmand = "-_";
                }
            }

            byte var10 = -1;
            switch(conmand.hashCode()) {
                case 1490:
                    if (conmand.equals("-_")) {
                        var10 = 8;
                    }
                    break;
                case 1984:
                    if (conmand.equals(">>")) {
                        var10 = 4;
                    }
                    break;
                case 1534362:
                    if (conmand.equals("2-10")) {
                        var10 = 1;
                    }
                    break;
                case 1534368:
                    if (conmand.equals("2-16")) {
                        var10 = 3;
                    }
                    break;
                case 2194663:
                    if (conmand.equals("GPS1")) {
                        var10 = 5;
                    }
                    break;
                case 3392903:
                    if (conmand.equals("null")) {
                        var10 = 0;
                    }
                    break;
                case 43092807:
                    if (conmand.equals("-2-10")) {
                        var10 = 2;
                    }
                    break;
                case 110364420:
                    if (conmand.equals("time1")) {
                        var10 = 6;
                    }
                    break;
                case 1379085786:
                    if (conmand.equals("-_byte")) {
                        var10 = 7;
                    }
            }

            switch(var10) {
                case 0:
                    result = this.source(result);
                    break;
                case 1:
                    result = this.trans2to10(result);
                    break;
                case 2:
                    result = this.transSymbol2to10(result);
                    break;
                case 3:
                    result = this.trans2to16(result);
                    break;
                case 4:
                    result = this.divide10Square(result, number);
                    break;
                case 5:
                    result = this.getGPS1(result);
                    break;
                case 6:
                    result = this.getGPS1(result);
                    break;
                case 7:
                    result = this.reverseByte(result);
                    break;
                case 8:
                    result = this.reverse(result, number);
                    break;
                default:
                    System.out.println("无法识别的指令：" + conmand);
            }
        }

        return result;
    }

    private String source(String value) {
        return value;
    }

    private String trans16to2(String hexString) {
        int num = 2;
        if (hexString != null && hexString.length() % num == 0) {
            String bString = "";

            for(int i = 0; i < hexString.length(); ++i) {
                String tmp = "0000" + Integer.toBinaryString(Integer.parseInt(hexString.substring(i, i + 1), 16));
                bString = bString + tmp.substring(tmp.length() - 4);
            }

            return bString;
        } else {
            return null;
        }
    }

    private String trans2to16(String binaryStr) {
        byte num;
        for(num = 4; binaryStr.length() % num != 0; binaryStr = "0" + binaryStr) {
        }

        StringBuffer sbs = new StringBuffer();

        for(int i = 0; i < binaryStr.length() / num; ++i) {
            String subStr = binaryStr.substring(i * num, i * num + num);
            String hexStr = Integer.toHexString(Integer.parseInt(subStr, 2));
            sbs.append(hexStr);
        }

        return sbs.toString();
    }

    public String trans2to10(String source) {
        String result = String.valueOf(Integer.parseInt(source, 2));
        return result;
    }

    public String transSymbol2to10(String source) {
        String result = source.substring(1);
        if (source.startsWith("1")) {
            StringBuilder goal = new StringBuilder();

            int i;
            for(i = 0; i < result.length(); ++i) {
                if ("0".equals(result.substring(i, i + 1))) {
                    goal.append("1");
                } else {
                    goal.append("0");
                }
            }

            i = Integer.parseInt(goal.toString(), 2) + 1;
            result = "-" + i;
        } else {
            result = String.valueOf(Integer.parseInt(source.substring(1), 2));
        }

        return result;
    }

    private String divide10Square(String source, String num) {
        StringBuilder result = new StringBuilder(source);
        int n = num.length() - 1;
        if (source.length() <= n) {
            for(int i = 0; i < n - source.length(); ++i) {
                result.insert(0, "0");
            }

            result.insert(0, "0.");
        } else {
            result.insert(source.length() - n, ".");
        }

        return result.toString();
    }

    public String getGPS1(String source) {
        int length = 32;
        if (source.length() != length) {
            System.out.println("长度不符合GPS1转化条件");
            return null;
        } else {
            Double result = (double)Long.parseLong(source, 2) / 600000.0D;
            return String.valueOf(result);
        }
    }

    private String getTime1(String time) {
        int length = 48;
        if (time.length() != length) {
            System.out.println("长度不符合time1转化条件");
            return null;
        } else {
            int year = 2000 + Integer.parseInt(time.substring(0, 8), 2);
            int month = Integer.parseInt(time.substring(8, 16), 2);
            int day = Integer.parseInt(time.substring(16, 24), 2);
            int hour = Integer.parseInt(time.substring(24, 32), 2);
            int minute = Integer.parseInt(time.substring(32, 40), 2);
            int second = Integer.parseInt(time.substring(40, 48), 2);
            Calendar c = Calendar.getInstance();
            c.set(year, month - 1, day, hour, minute, second);
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String result = formatter.format(c.getTime());
            return result;
        }
    }

    private String reverseByte(String source) {
        int n = source.length() / 2;
        StringBuilder reslut = new StringBuilder();

        for(int i = 0; i < n; ++i) {
            reslut.append(source.substring(i * 2 + 1, i * 2 + 2)).append(source.substring(i * 2, i * 2 + 1));
        }

        return String.valueOf(reslut);
    }

    private String reverse(String source, String num) {
        int x = Integer.parseInt(num);
        int n = source.length() / x;
        StringBuilder result = new StringBuilder();

        for(int i = n; i > 0; --i) {
            result.append(source.substring((i - 1) * x, i * x));
        }

        return result.toString();
    }
}